<?php

class Application_Form_Informacio extends Zend_Form
{

    public function init()
    {
        $this->setAttrib('id', 'frmProductes')        	 
        	 ->setEnctype('multipart/form-data')
        	 ->setMethod('post');
       	
       	$this->addElement('hidden', 'id_producto', array('label' =>'Id Producte'));
       	
       	$this->addElement('hidden', 'tipo_registro', array(
       	'label' 	=> 'Tipus registre',
       	'filters'   => array('StringTrim'),
       	'required'  => true,
    	'size'		=> 10,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(1, 1))
                )
       	));
       	
       	$this->addElement('text', 'titulo', array(
       	'label' 	=> 'Títol',
       	'filters'   => array('StringTrim'),
       	'required'  => true,
    	'size'		=> 60,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(3, 30))
                )
       	));
       	$this->addElement('textarea', 'descripcion', array(
       	'label' 	=> 'Descripció',
       	'filters'   => array('StringTrim'),
    	'size'		=> 60,
       	'cols'   	=> 44,
       	'rows'   	=> 5,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(3, 512))
                )
       	));
       	
       	$this->addElement('text', 'direccion', array(
       	'label' 	=> 'Adreça',
       	'filters'   => array('StringTrim'),
    	'size'		=> 60,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(3, 60))
                )
       	));
       	
//       	$element = new Zend_Form_Element_File('file');
// 		$element->setLabel('Pujar imatge:');		
// 		// ensure only 1 file
// 		$element->addValidator('Count', false, 1);		
// 		$element->addValidator('Size', false, 102400);		
// 		$element->addDecorator('ViewHelper');		
// 		$element->addValidator('Extension', false, 'jpg');		
// 		$this->addElement($element, 'file');
        
		$this->addElement('hidden', 'imagen', array(
       	'label' 	=> 'Imagen',
       	'filters'   => array('StringTrim'),
    	'size'		=> 60,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(1, 512))
                )
       	));
		
      	$element = new Zend_Form_Element_File('document');       	
		$element->setLabel('Pujar document:');		
		$element->addValidator('Count', false, 1);		
		$element->addValidator('Size', false, 1024000);		
		$element->addDecorator('ViewHelper');		
		$element->addValidator('Extension', false, 'pdf');		
		$this->addElement($element, 'document');
		
		$this->addElement('hidden', 'documento', array(
       	'label' 	=> 'Document',
       	'filters'   => array('StringTrim'),
    	'size'		=> 60,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(1, 512))
                )
       	));
       	
       	$date = new Zend_Form_Element_Hidden('fecha');
		$date->setLabel('Data')
		     ->addValidator('Date', true,
		     	array('messages' => array('dateInvalidDate' => 'La data no es correcte'))
		     )
		     ->addValidator('NotEmpty', true, array('messages' => array('isEmpty' => "El camp data no pot estar buit")))
		     ->setRequired(true);
       	$this->addElement($date);
       	
       	$this->addElement('hidden', 'tipo_producto', array(
       	'label' 	=> 'Tipus producte',       	
    	'size'		=> 40       	
       	));       	
       
       	
       	$this->addElement('hidden', 'empresa', array(
       	'label' 	=> 'Empresa',
    	'size'		=> 10,
       	'required'  => true,
       	'validators'=> array(
                array('validator' => 'int')
                )
       	));
       	
       	$this->addElement('text', 'contacto', array(
       	'label' 	=> 'Contacte',
       	'filters'   => array('StringTrim'),
    	'size'		=> 60,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(3, 100))
                )
       	));
       	
       	$this->addElement('text', 'telefono', array(
       	'label' 	=> 'Telèfon',
       	'filters'   => array('StringTrim'),
    	'size'		=> 60,
       	'validators'=> array(
                array('validator' => 'StringLength', 'options' => array(3, 15))
                )
       	));
       	
       	$this->addElement('text', 'email', array(
       	'label' 	=> 'E-Mail',
       	'filters'   => array('StringTrim'),
    	'size'		=> 60,
       	'validators'=> array(
       						 array('validator' => 'StringLength', 'options' => array(3, 50)),
       						 array('validator' => 'EmailAddress')
       						 )
       	));
       	
       	$this->addElement('hidden', 'oferta_lugar', array(
       	'label' 	=> 'Lloc',
    	'size'		=> 10,
       	'required'  => true,
       	'validators'=> array(
                array('validator' => 'int')
                )
       	));
       	
       	$this->addElement('hidden', 'oferta_tipo', array(
       	'label' 	=> 'Tipus',
    	'size'		=> 10,
       	'required'  => true,
       	'validators'=> array(
                array('validator' => 'int')
                )
       	));
       	
       	$this->addElement('submit', 'submit', array('label' => 'Guardar y tancar'));
    }
    
    public function showErrors($arrMessages)
    {   
    	$valores = '';
	    foreach ($arrMessages as $key => $value) {
    		foreach ($arrMessages[$key] as $value2) {
    			$label = $this->getElement($key)->getLabel();
    			$valores.= "<li>El camp $label no es correcte. Error:$value2</li>";	
    		}
    	}
    	//print_r($valores);
		return "<ul>".$valores."</ul>";
    }

}

